﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.ValentineUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import org.aswing.geom.*;
    import Display.*;
    import flash.text.*;
    import Modules.achievements.data.*;
    import Display.aswingui.*;
    import Modules.achievements.events.*;

    public class AchievementsCell extends DataItemCell {

        protected const ICON_WIDTH:int = 64;
        protected const ICON_HEIGHT:int = 64;

        protected var m_achieve:Achievement;
        protected var m_bg:DisplayObject;
        protected var m_position:int;
        protected var m_iconPanel:JPanel;
        protected var m_headerPanel:JPanel;
        protected var m_rewardPanel:JPanel;
        protected var m_claimPanel:JPanel;
        protected var m_iconLoader:Loader;
        protected var m_icon:DisplayObject;
        protected var m_rewardLoader:Loader;
        protected var m_reward:DisplayObject;
        protected var m_dummy:Boolean = false;

        public function AchievementsCell(_arg1:int=0){
            this.m_position = _arg1;
            super(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, 0, SoftBoxLayout.TOP));
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage, false, 0, true);
        }
        protected function onAddedToStage(_arg1:Event):void{
            this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage, false, 0, true);
            Global.achievementsManager.addEventListener(AchievementGroupsUpdatedEvent.UPDATED, this.refreshCell, false, 0, true);
        }
        protected function onRemovedFromStage(_arg1:Event):void{
            Global.achievementsManager.removeEventListener(AchievementGroupsUpdatedEvent.UPDATED, this.refreshCell);
            this.removeEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
        }
        override public function setCellValue(_arg1):void{
            this.m_achieve = (_arg1 as Achievement);
            if (this.m_achieve.id == null){
                this.m_dummy = true;
            };
            this.buildCell();
            this.loadAssets();
        }
        protected function refreshCell(_arg1:Event):void{
            this.removeAll();
            this.buildCell();
            this.loadAssets();
        }
        protected function loadAssets():void{
            if (this.m_dummy == false){
                if (this.m_achieve.iconUrl){
                    this.m_iconLoader = LoadingManager.load(Global.getAssetURL(this.m_achieve.iconUrl), this.onIconLoad, LoadingManager.PRIORITY_HIGH);
                };
                if (this.m_achieve.rewards["item"] != null){
                    this.m_rewardLoader = LoadingManager.load(Global.gameSettings.getImageByName(this.m_achieve.rewards["item"], "icon"), this.onRewardLoad, LoadingManager.PRIORITY_HIGH);
                };
            };
        }
        protected function buildCell():void{
            this.m_iconPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            this.m_headerPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            this.m_rewardPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            this.m_claimPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.buildBackground();
            if (this.m_dummy == false){
                this.makeIconPanel();
                this.makeHeaderPanel();
                this.makeRewardPanel();
                this.makeClaimPanel();
            };
            ASwingHelper.prepare(this);
        }
        protected function buildBackground():void{
            var _local1:DisplayObject = (new (ValentineDialog.assetDict["left_blue"])() as DisplayObject);
            var _local2:DisplayObject = (new (ValentineDialog.assetDict["middle_blue"])() as DisplayObject);
            var _local3:DisplayObject = (new (ValentineDialog.assetDict["middle_white"])() as DisplayObject);
            var _local4:DisplayObject = (new (ValentineDialog.assetDict["right_white"])() as DisplayObject);
            var _local5:int = (this.m_position % 4);
            if (_local5 == 1){
                this.m_bg = _local1;
            } else {
                if (_local5 == 2){
                    this.m_bg = _local3;
                } else {
                    if (_local5 == 3){
                        this.m_bg = _local2;
                    } else {
                        this.m_bg = _local4;
                    };
                };
            };
            var _local6:MarginBackground = new MarginBackground(this.m_bg);
            this.setBackgroundDecorator(_local6);
            this.setPreferredSize(new IntDimension(this.m_bg.width, this.m_bg.height));
            this.setMinimumSize(new IntDimension(this.m_bg.width, this.m_bg.height));
            this.setMaximumSize(new IntDimension(this.m_bg.width, this.m_bg.height));
        }
        protected function makeIconPanel():void{
            this.m_iconPanel.setPreferredHeight(this.ICON_HEIGHT);
            this.m_iconPanel.setMinimumHeight(this.ICON_HEIGHT);
            this.m_iconPanel.setMaximumHeight(this.ICON_HEIGHT);
            this.appendAll(ASwingHelper.verticalStrut(10), this.m_iconPanel);
        }
        protected function makeHeaderPanel():void{
            var _local4:String;
            var _local1:uint = EmbeddedArt.blueTextColor;
            if (this.m_achieve.state == Achievement.REWARDED){
                _local1 = EmbeddedArt.lightGrayTextColor;
            };
            var _local2:String = ZLoc.t("Achievements", this.m_achieve.displayTitle);
            var _local3:AssetPane = ASwingHelper.makeMultilineCapsText(_local2, 130, EmbeddedArt.TITLE_FONT, TextFormatAlign.CENTER, 13, _local1);
            if (this.m_achieve.state == Achievement.FINISHED){
                _local4 = ZLoc.t("Achievements", "Finished_Message");
            } else {
                if (this.m_achieve.state == Achievement.REWARDED){
                    _local4 = ZLoc.t("Achievements", "Rewarded_Message");
                } else {
                    _local4 = ZLoc.t("Achievements", this.m_achieve.displayDescription);
                };
            };
            var _local5:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            var _local6:AssetPane = ASwingHelper.makeMultilineText(_local4, 130, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.LEFT, 10, _local1);
            _local5.appendAll(ASwingHelper.horizontalStrut(3), _local6);
            this.m_headerPanel.appendAll(_local3, _local5);
            this.m_headerPanel.setPreferredHeight(this.ICON_HEIGHT);
            this.m_headerPanel.setMinimumHeight(this.ICON_HEIGHT);
            this.m_headerPanel.setMaximumHeight(this.ICON_HEIGHT);
            this.appendAll(ASwingHelper.verticalStrut(10), this.m_headerPanel);
        }
        protected function makeRewardPanel():void{
            var _local1:DisplayObject;
            var _local2:DisplayObject;
            var _local3:MarginBackground;
            var _local4:ASColor;
            var _local5:DisplayObject;
            var _local6:JLabel;
            var _local7:JPanel;
            var _local8:DisplayObject;
            var _local9:JLabel;
            var _local10:JPanel;
            var _local11:DisplayObject;
            var _local12:JLabel;
            var _local13:JPanel;
            var _local14:DisplayObject;
            var _local15:JLabel;
            var _local16:JPanel;
            if (this.m_achieve.rewards["item"] != null){
                _local1 = (new (ValentineDialog.assetDict["rewardCard_locked"])() as DisplayObject);
                _local2 = (new (ValentineDialog.assetDict["rewardCard_unlocked"])() as DisplayObject);
                if (this.m_achieve.state == Achievement.REWARDED){
                    _local3 = new MarginBackground(_local1, new Insets(0, 26, 6, 26));
                } else {
                    _local3 = new MarginBackground(_local2, new Insets(0, 26, 6, 26));
                };
                this.m_rewardPanel.setBackgroundDecorator(_local3);
            } else {
                _local4 = new ASColor(EmbeddedArt.blueTextColor);
                if (this.m_achieve.state == Achievement.REWARDED){
                    _local4 = new ASColor(EmbeddedArt.lightGrayTextColor);
                };
                if (this.m_achieve.rewards["xp"] != null){
                    _local5 = (new EmbeddedArt.smallXPIcon() as DisplayObject);
                    _local6 = new JLabel(ZLoc.t("Dialogs", "NumXP", {num:this.m_achieve.rewards["xp"]}));
                    _local6.setFont(ASwingHelper.getBoldFont(14));
                    _local6.setForeground(_local4);
                    _local7 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
                    _local7.appendAll(ASwingHelper.horizontalStrut(15), new AssetPane(_local5));
                    _local7.appendAll(ASwingHelper.horizontalStrut(5), _local6);
                    this.m_rewardPanel.append(_local7);
                };
                if (this.m_achieve.rewards["goods"] != null){
                    _local8 = (new EmbeddedArt.smallGoodsIcon() as DisplayObject);
                    _local9 = new JLabel(ZLoc.t("Dialogs", "NumGoods", {num:this.m_achieve.rewards["goods"]}));
                    _local9.setFont(ASwingHelper.getBoldFont(14));
                    _local9.setForeground(_local4);
                    _local10 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
                    _local10.appendAll(ASwingHelper.horizontalStrut(15), new AssetPane(_local8));
                    _local10.appendAll(ASwingHelper.horizontalStrut(5), _local9);
                    this.m_rewardPanel.append(_local10);
                };
                if (this.m_achieve.rewards["coins"] != null){
                    _local11 = (new EmbeddedArt.smallCoinIcon() as DisplayObject);
                    _local12 = new JLabel(ZLoc.t("Dialogs", "NumCoins", {num:this.m_achieve.rewards["coins"]}));
                    _local12.setFont(ASwingHelper.getBoldFont(14));
                    _local12.setForeground(_local4);
                    _local13 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
                    _local13.appendAll(ASwingHelper.horizontalStrut(15), new AssetPane(_local11));
                    _local13.appendAll(ASwingHelper.horizontalStrut(5), _local12);
                    this.m_rewardPanel.append(_local13);
                };
                if (this.m_achieve.rewards["energy"] != null){
                    _local14 = (new EmbeddedArt.smallEnergyIcon() as DisplayObject);
                    _local15 = new JLabel(ZLoc.t("Dialogs", "NumEnergy", {num:this.m_achieve.rewards["energy"]}));
                    _local15.setFont(ASwingHelper.getBoldFont(14));
                    _local15.setForeground(_local4);
                    _local16 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
                    _local16.appendAll(ASwingHelper.horizontalStrut(15), new AssetPane(_local14));
                    _local16.appendAll(ASwingHelper.horizontalStrut(5), _local15);
                    this.m_rewardPanel.append(_local16);
                };
            };
            this.m_rewardPanel.setPreferredHeight(100);
            this.m_rewardPanel.setMinimumHeight(100);
            this.m_rewardPanel.setMaximumHeight(100);
            this.appendAll(ASwingHelper.verticalStrut(6), this.m_rewardPanel);
        }
        protected function makeClaimPanel():void{
            var _local2:CustomButton;
            var _local3:String;
            var _local4:AssetPane;
            var _local5:JLabel;
            var _local6:AssetIcon;
            var _local7:JPanel;
            var _local8:Sprite;
            var _local9:DisplayObject;
            var _local10:AssetPane;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            if (Global.achievementsManager.isUpdated){
                if (this.m_achieve.state == Achievement.STARTED){
                    _local3 = ZLoc.t("Dialogs", "ValUI_unlocknow");
                    _local5 = new JLabel(ZLoc.t("Dialogs", "ValUI_unlocknow"));
                    _local5.setForeground(new ASColor(EmbeddedArt.blueTextColor));
                    _local5.setFont(ASwingHelper.getBoldFont(12));
                    _local6 = new AssetIcon((new EmbeddedArt.icon_cash() as DisplayObject));
                    _local7 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
                    _local2 = new CustomButton(String(this.m_achieve.cashcost), _local6, "CashButtonUI");
                    _local2.addEventListener(MouseEvent.CLICK, this.onCashBuy, false, 0, true);
                    _local7.append(_local2);
                    _local1.appendAll(_local5, _local7);
                } else {
                    if (this.m_achieve.state == Achievement.REWARDED){
                        _local8 = new Sprite();
                        _local9 = (new (ValentineDialog.assetDict["rewardClaimed"])() as DisplayObject);
                        _local8.addChild(_local9);
                        _local10 = new AssetPane(_local8);
                        _local8.x = ((_local1.width / 2) - (_local9.x / 2));
                        _local1.append(_local10);
                    } else {
                        if (this.m_achieve.state == Achievement.FINISHED){
                            _local3 = ZLoc.t("Dialogs", "Collect").toUpperCase();
                            _local2 = new CustomButton(_local3, null, "GreenButtonUI");
                            _local2.setFont(ASwingHelper.getTitleFont(13));
                            _local2.addEventListener(MouseEvent.MOUSE_UP, this.onCollect, false, 0, true);
                            _local1.appendAll(ASwingHelper.verticalStrut(17), _local2);
                        };
                    };
                };
                if (_local2){
                    _local2.setPreferredSize(new IntDimension(110, 30));
                    _local2.setMinimumSize(new IntDimension(110, 30));
                    _local2.setMaximumSize(new IntDimension(110, 30));
                };
            };
            this.m_claimPanel.append(_local1);
            this.append(this.m_claimPanel);
        }
        override protected function onIconLoad(_arg1:Event):void{
            if (((this.m_iconLoader) && (this.m_iconLoader.content))){
                this.m_icon = this.m_iconLoader.content;
            };
            this.m_icon.scaleX = (this.m_icon.scaleY = scaleToFit(this.m_icon));
            if ((this.m_icon is Bitmap)){
                (this.m_icon as Bitmap).smoothing = true;
            };
            this.placeIcon();
            setGridListCellStatus(m_gridList, false, m_index);
        }
        protected function onRewardLoad(_arg1:Event):void{
            if (((this.m_rewardLoader) && (this.m_rewardLoader.content))){
                this.m_reward = this.m_rewardLoader.content;
            };
            this.m_reward.scaleX = (this.m_reward.scaleY = scaleToFit(this.m_reward));
            if ((this.m_reward is Bitmap)){
                (this.m_reward as Bitmap).smoothing = true;
            };
            this.placeReward();
            setGridListCellStatus(m_gridList, false, m_index);
        }
        protected function placeIcon():void{
            var _local1:Sprite = new Sprite();
            _local1.addChild(this.m_icon);
            this.m_iconPanel.addChild(_local1);
            _local1.x = ((this.m_iconPanel.width / 2) - (this.m_icon.width / 2));
            ASwingHelper.prepare(this);
        }
        protected function placeReward():void{
            var _local1:Sprite = new Sprite();
            _local1.addChild(this.m_reward);
            this.m_rewardPanel.addChild(_local1);
            if (this.m_achieve.state == Achievement.REWARDED){
                _local1.alpha = 0.5;
            };
            _local1.x = ((this.m_rewardPanel.width / 2) - (this.m_reward.width / 2));
            _local1.y = ((this.m_rewardPanel.height / 2) - (this.m_reward.height / 2));
            ASwingHelper.prepare(this);
        }
        protected function onCollect(_arg1:Event):void{
            if ((((this.m_achieve == null)) || (!((this.m_achieve.state == Achievement.FINISHED))))){
                return;
            };
            Global.achievementsManager.claimReward(this.m_achieve.groupName, this.m_achieve.id);
            if (this.m_achieve.rewards["item"] == null){
                UI.displayValRewardsMessage(ZLoc.t("Dialogs", "ValUI_rewardClaimed_stats"), GenericDialogView.TYPE_OK, null, "ValUI_rewards", false, null, "ValUI_rewards");
            } else {
                UI.displayValRewardsMessage(ZLoc.t("Dialogs", "ValUI_rewardClaimed_items"), GenericDialogView.TYPE_OK, null, "ValUI_rewards", false, null, "ValUI_rewards");
            };
            this.removeAll();
            this.m_achieve.state = Achievement.REWARDED;
            this.buildCell();
            this.loadAssets();
        }
        protected function onCashBuy(_arg1:Event):void{
            if ((((this.m_achieve == null)) || (!((this.m_achieve.state == Achievement.STARTED))))){
                return;
            };
            var _local2:Boolean = Global.achievementsManager.purchaseFinish(this.m_achieve.groupName, this.m_achieve.id);
            if (_local2){
                this.m_achieve.state = Achievement.FINISHED;
                this.onCollect(_arg1);
                this.maybeRefreshAchievements();
            } else {
                UI.displayPopup(new GetCoinsDialog(ZLoc.t("Dialogs", "ImpulseMarketCash"), "unlockAchievement", GenericDialogView.TYPE_GETCASH, null, true));
            };
        }
        protected function maybeRefreshAchievements():void{
            Global.achievementsManager.startGroupUpdate("vday_2011");
            TransactionManager.sendAllTransactions(true);
        }

    }
}//package Display.ValentineUI 
